package com.sharemarking.wa.platform.service.OrderCommonFactory;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sharemarking.wa.common.entity.Member;
import com.sharemarking.wa.common.entity.Order;
import com.sharemarking.wa.common.eum.OrderOperateTypeEum;
import com.sharemarking.wa.common.eum.OrderStateEum;
import com.sharemarking.wa.common.eum.OrderTypeEum;
import com.sharemarking.wa.platform.dto.orderCommon.*;
import com.sharemarking.wa.platform.mapper.*;
import com.sharemarking.wa.platform.service.SecurityService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author dxy
 */
@Component
public class ProductWaste implements OrderCommon {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private SecurityService ss;
    @Autowired
    private OrgMapper orgMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderAddressMapper orderAddressMapper;
    @Autowired
    private OrgMemberMapper orgMemberMapper;
    @Autowired
    private DriverVerifyMapper driverVerifyMapper;
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private OrderCommonMapper orderCommonMapper;

    @Override
    public List<OrderListDto> getOrderList(Map params) {
        List<OrderListDto> list = orderCommonMapper.getList(params);

        for (OrderListDto m : list) {
            //重新设置订单状态：待待司机确认=>待运
            if (m.getState().equals(OrderStateEum.DRIVER_CONFIRM.getKey())) {
                m.setState(OrderStateEum.NO_TRANSPORT.getKey());
            }
            List<OrderOperateTypeEum> eumList = new ArrayList<>();
            //获取废品订单
            m.setWasteList(orderCommonMapper.getMyOrderWasteListByAllOrder(m.getId()));
            if (m.getType() == 0) {//总订单
                //判断状态
                Integer childCount = orderMapper.selectCount(new QueryWrapper<Order>().eq("parent_id", m.getId()));
                if (childCount == 0 && m.getState().equals(OrderStateEum.UNTREATED.getKey())) {
                    //待处理状态且未有子订单：可以取消订单
                    eumList.add(OrderOperateTypeEum.CANCEL);
                } else if (childCount > 0 && m.getState().equals(OrderStateEum.UNTREATED.getKey())) {
                    //待处理状态且有子订单：只能完结订单
                    eumList.add(OrderOperateTypeEum.END);
                } else if (m.getState().equals(OrderStateEum.END.getKey()) || m.getState().equals(OrderStateEum.CANCEL.getKey())) {
                    //已取消状态和已完结状态：重新发布订单
                    eumList.add(OrderOperateTypeEum.REPUBLISH);
                }
                m.setOperate(eumList);
            } else if (m.getType() == 1) {//子订单
                //判断状态
                if (m.getState().equals(OrderStateEum.RECEIPT.getKey())) {
                    eumList.add(OrderOperateTypeEum.CANCEL);
                    m.setOperate(eumList);
                } else if (m.getPayer() != null && m.getPayer() == 1 && m.getState().equals(OrderStateEum.UNPAID.getKey())) {
                    eumList.add(OrderOperateTypeEum.CANCEL);
                    eumList.add(OrderOperateTypeEum.PAY);
                    m.setOperate(eumList);
                }
            }
        }

        return list;
    }

    @Override
    public OrderInfoDto getOrderInfo(Map params) {
        String code = String.valueOf(params.get("code"));

        Order order = orderMapper.selectOne(new QueryWrapper<Order>().eq("code", code));
        OrderInfoDto orderInfoDto = new OrderInfoDto();
        if (order != null) {
            BeanUtils.copyProperties(order, orderInfoDto);
        }
        if (order.getType() .equals(OrderTypeEum.PARENT_TYPE.getKey())) {//总订单
            //查找子订单
            orderInfoDto.setWasteList(orderCommonMapper.getMyOrderWasteListByAllOrder(order.getId()));
            List<OrderOperateTypeEum> eumList = new ArrayList<>();

            //获取装货地址
            String loadingAddress = orderAddressMapper.getAddressByOrderId(order.getId(), 0);
            orderInfoDto.setLoadingAddress(loadingAddress);

            Integer childCount = orderMapper.selectCount(new QueryWrapper<Order>().eq("parent_id", order.getId()));
            if (childCount == 0 && order.getState().equals(OrderStateEum.UNTREATED.getKey())) {
                //待处理状态且未有子订单：可以取消订单
                eumList.add(OrderOperateTypeEum.CANCEL);
            } else if (childCount > 0 && order.getState().equals(OrderStateEum.UNTREATED.getKey())) {
                //待处理状态且有子订单：只能完结订单
                eumList.add(OrderOperateTypeEum.END);
            } else if (order.getState().equals(OrderStateEum.END.getKey()) || order.getState().equals(OrderStateEum.CANCEL.getKey())) {
                //已取消状态和已完结状态：重新发布订单
                eumList.add(OrderOperateTypeEum.REPUBLISH);
            }

            orderInfoDto.setOperate(eumList);

        } else if (order.getType().equals(OrderTypeEum.CHILDREN_TYPE.getKey())) {
            //子订单
            //获取装货地址
            String loadingAddress = orderAddressMapper.getAddressByOrderId(order.getId(), 0);
            //获取卸货地址
            String unLoadingAddress = orderAddressMapper.getAddressByOrderId(order.getId(), 1);

            orderInfoDto.setLoadingAddress(loadingAddress);
            orderInfoDto.setUnLoadingAddress(unLoadingAddress);

            //完成时间 和 到达时间
            orderInfoDto.setArrivalTime(order.getArrivalTime());
            orderInfoDto.setCompleteTime(order.getCompleteTime());

            //获取经营单位名称
            if (!StringUtils.isEmpty(order.getReceiveOrgId())) {
                orderInfoDto.setOrgName(orgMapper.getOrgNameById(order.getReceiveOrgId()));
            }

            //获取经营单位联系人姓名和电话
            if (!StringUtils.isEmpty(order.getReceiveOrgMemberId())) {
                Member member = memberMapper.get(order.getReceiveOrgMemberId());
                if (member.getRealName() != null) {
                    orderInfoDto.setContact(member.getRealName());
                } else {
                    orderInfoDto.setContact(member.getNickName());
                }
                orderInfoDto.setMobile(member.getMobile());
            }

            //获取运输单位名称
            if (!StringUtils.isEmpty(order.getTransportOrgId())) {
                orderInfoDto.setTransportOrgName(orgMapper.getOrgNameById(order.getTransportOrgId()));
            }

            //获取司机名称和电话
            orderInfoDto.setCarWeight(order.getCarWeight());
            if (!StringUtils.isEmpty(order.getDriverId())) {
                Member member = memberMapper.get(order.getDriverId());
                if (member != null) {
                        orderInfoDto.setDriverName(member.getRealName() != null?member.getRealName():member.getNickName());
                    orderInfoDto.setDriverMobile(member.getMobile());
                }
                orderInfoDto.setCarNumber(driverVerifyMapper.getCarMemberIdByMemberId(order.getDriverId()));
            }

            //获取废品列表
            List<WasteListDto> childOrderWasteListDtos = orderCommonMapper.getMyOrderWasteListByAllOrder(order.getId());
            orderInfoDto.setWasteList(childOrderWasteListDtos);

            Member member;
            //获取产废单位信息
            orderInfoDto.setWasteOrgName(orgMapper.getOrgNameById(order.getSendOrgId()));
            //获取产废单位联系人 手机号码
            member = memberMapper.get(order.getSendOrgMemberId());
            if (member.getRealName() != null) {
                orderInfoDto.setWasteContact(member.getRealName());
            } else {
                orderInfoDto.setWasteContact(member.getNickName());
            }
            orderInfoDto.setWasteMobile(member.getMobile());

            //获取经营单位联系人姓名和电话
            if (!StringUtils.isEmpty(order.getReceiveOrgMemberId())) {
                member = memberMapper.get(order.getReceiveOrgMemberId());
                if (member.getRealName() != null) {
                    orderInfoDto.setContact(member.getRealName());
                } else {
                    orderInfoDto.setContact(member.getNickName());
                }
                orderInfoDto.setMobile(member.getMobile());
            }

            orderInfoDto.setCarRequirement(order.getCarRequirement());

            //判断状态
            List<OrderOperateTypeEum> eumList = new ArrayList<>();
            if (order.getState().equals(OrderStateEum.RECEIPT.getKey())) {
                //待经营商接单状态：取消订单
                eumList.add(OrderOperateTypeEum.CANCEL);
            } else if (order.getPayer() != null && order.getPayer() == 1 && order.getState().equals(OrderStateEum.UNPAID.getKey())) {
                //代付款状态：取消订单和付款
                eumList.add(OrderOperateTypeEum.CANCEL);
                eumList.add(OrderOperateTypeEum.PAY);
            } else if (order.getState().equals(OrderStateEum.TRANSPORT_RECEIPT.getKey())) {
                //待运输接单状态：取消订单
                eumList.add(OrderOperateTypeEum.CANCEL);
            }

            orderInfoDto.setOperate(eumList);
        }


        return orderInfoDto;


    }

    @Override
    public List<TaskOrderListDto> getTaskOrderList(Map params) {
        return null;
    }

    @Override
    public OrderInfoDto getTaskOrderInfo(Map params) {
        return null;
    }

}
